<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Archive extends Controller
{
	private $_template_f;
	private $_sess_id;
	private $_lang;
	function __construct()
	{
		parent::Controller();
		$this->_template_f = $this->config->item('template_f');
		$this->_lang = $this->mycommon->getLangCode();
		if(!$this->session->userdata('uname'))
		{
			redirect($this->config->item('base_url'));
			die();
		}
		$this->load->model('archive/archive_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('archive/archive', $this->_lang);
	}
	
	function _loadHeader($data)
	{
		$header = array();
		$header['title'] = $data['title'];
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['module_name'] = 'contact';
		$header['lang'] = $this->_lang;
		$header['common_lang'] = $this->lang->line('common_lang');
		
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
        
		$this->load->view($this->_template_f . 'menu_view',$data);
	}
    	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function index()
	{
        $data['archive_lang'] = $archive_lang = $this->lang->line('archive_lang');
        $data['title']=$data['archive_lang']['archive'];
        //load header
  		$this->_loadHeader($data);
  		//load menu
        $this->_loadMenu();
        
  		if(!isset($todate))
        {
            $data['todate']=$todate=date('Y-m-d');
            $fromdate = strtotime ( '-7 day' , strtotime ( $todate ) ) ;
            $data['fromdate']=$fromdate = date ( 'Y-m-d' , $fromdate );
        }
        $userid=$this->session->userdata('uid');
        $data['status']=$status=0;
        $data['page'] = $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['numrow'] = $numrow = $this->input->get('numrow') ? $this->input->get('numrow') : '20';
        
        $groupid = $this->input->get('group');
		$groupid = is_numeric($groupid) && $groupid != '' ? $groupid :-1;
        $temp = $this->archive_model->selfserving_admin_get_all_banners($status,$fromdate,$todate,$userid,$page,$numrow,'',$groupid);
        $data['info'] = $temp['info'];
        $data['totalpage'] = $temp['total'][0]['totalpage'];
  		$this->load->view($this->_template_f.'archive/archive_view',$data);
  		$this->_loadFooter();
	}
    function filter()
    {
        $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['page']=$page = (is_numeric($page) && $page > 0) ? $page : '1';
        
        $numrow = $this->input->get('numrow') ? $this->input->get('numrow') : '20';
        $data['numrow']=$numrow = (is_numeric($numrow) && $numrow > 0) ? $numrow : '20';
        
        $groupid = $this->input->get('group');
		$groupid = is_numeric($groupid) && $groupid != '' ? $groupid :-1;
        //check status
        $data['status'] =$status = 0;
        //check fromdate
        $data['todate'] = $todate = $this->input->get('todate');
        if(is_date($todate)==false)
        {
            $data['todate']=$todate=date('Y-m-d');
        }
    	//check todate
        $data['fromdate'] = $fromdate = $this->input->get('fromdate');
        if(is_date($fromdate)==false)
        {
            $data['fromdate']=$fromdate = date ( 'Y-m-d' , strtotime ( '-7 day' , strtotime ( $todate ) ) );
        }
        
        $userid=$this->session->userdata('uid');
        $temp = $this->archive_model->selfserving_admin_get_all_banners($status,$fromdate,$todate,$userid,$page,$numrow,'',$groupid);
        $data['info'] = $temp['info'];
        $data['totalpage'] = $temp['total'][0]['totalpage'];     
        $data['archive_lang']=$this->lang->line('archive_lang');
		$this->load->view($this->_template_f.'archive/table_view',$data);
    }
    function deleteAd($bannerid)
    {        
        $type=4;
        $val=0;
        $count = $this->archive_model->selfserving_banner_update_inline($bannerid,$val,$type);
        if($count==true)
        {
            redirect(site_url('archive'));
        }else{
            echo 'Error,try again,please!';
        }      
    }
}